package com.changil.admin.action.board;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.changil.admin.action.Action;
import com.changil.admin.dto.InsertedId;
import com.changil.admin.protocol.ArticleManager;
import com.changil.admin.protocol.http.HttpUtil;

public class WriteAction extends Action {
	private int boardId;

	public WriteAction(int boardId) {
		this.boardId = boardId;
	}

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		try {
			String title = request.getParameter("title");
			String editor = request.getParameter("editor");
			InsertedId insertedId = ArticleManager.getsInstance().writeArticle(boardId, title, editor);
			Pattern p = Pattern.compile("<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>");
			Matcher m = p.matcher(editor);
			while (m.find()) {
				System.out.println(m.group(1));
			}
			
			HttpUtil.redirect(response, "/NowonChangilChurch_Admin/board?boardId="+boardId);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
